package com.F04.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.F04.actionForm.F0410ActionForm;
import com.dao.DataOperations;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.util.CommonTools;

public class F0410Dao  extends DataOperations {

	Logger log = Logger.getLogger(F0409Dao.class);

	public F0410Dao() {

	}

	public F0410Dao(Connection conn) {
		super(conn);
	}
	
	public List select(F0410ActionForm ActionForm){
		List rtnList = new ArrayList(); // 存放查询结果
		Map map = null;
		StringBuffer sqlWeek = new StringBuffer();
		String ManufacturingOrder = CommonTools.null2String(ActionForm.getManufacturingOrder());
		int i = 1;
		
		sqlWeek.append("select adyh.FUN_GetNumOfWeek(DATEADD(day, adyh.FUN_GetMaxDay(delivery_time_s1,delivery_time_s2, delivery_time_s3, delivery_time_s4)+7, GETDATE())) numOfWeek,");
		sqlWeek.append("          p.manufacturing_order,m.code material_code, m.[name] material_name,sum(nsd.quantity * p.group_qty) qty,adyh.FUN_GetNumOfWeek(getdate()) sysdate,adyh.FUN_GetNumOfWeek(p.[start_date]) - 1 needWeek");
		sqlWeek.append("      from  t_materials m,pc_materials_supplier ms, t_not_standard_products");
		sqlWeek.append("            nsp, t_not_standard_detail nsd, pd_planning p ");
		sqlWeek.append("      where nsp.code = nsd.standard_code and ");
		sqlWeek.append("            nsd.material_code = ms.material_code and");
		sqlWeek.append("            p.manufacturing_order = nsp.manufacturing_order and");
		sqlWeek.append("            m.code = nsd.material_code and");
		sqlWeek.append("            p.purchase_flag = '1' and ");
		sqlWeek.append("            adyh.FUN_GetNumOfWeek(p.[start_date]) - 1 < adyh.FUN_GetNumOfWeek(DATEADD(day,adyh.FUN_GetMaxDay(delivery_time_s1, delivery_time_s2,delivery_time_s3, delivery_time_s4)+7, GETDATE())) ");
		// 查询条件
		if (!"".equals(ManufacturingOrder)) {
			sqlWeek.append(" 		and p.manufacturing_order = '"
					+ ManufacturingOrder + "'");
		}
		sqlWeek.append("      group by p.manufacturing_order,m.code,m.name,adyh.FUN_GetNumOfWeek(DATEADD(day, adyh.FUN_GetMaxDay(delivery_time_s1,delivery_time_s2, delivery_time_s3, delivery_time_s4)+7, GETDATE())),adyh.FUN_GetNumOfWeek(p.[start_date]) - 1");
		
		ResultSet rest = null;
		try {
			rest = super.executeQuery(sqlWeek.toString());
			while (rest.next()) {
				map = new HashMap();
				map.put("arriveTime", CommonTools.null2String(rest.getString("numOfWeek")));
//				map.put("id", i+"");
				map.put("materialCode", CommonTools.null2String(rest.getString("material_code")));
				map.put("materialName", CommonTools.null2String(rest.getString("material_name")));
				map.put("ManufacturingOrder", CommonTools.null2String(rest.getString("manufacturing_order")));
				map.put("qty", String.valueOf(rest.getInt("qty")));
				map.put("needWeek", String.valueOf(rest.getInt("needWeek")));
				rtnList.add(map);
//				i++;
				if(String.valueOf(ActionForm.getCurrentWeek()) == null || "0".equals(String.valueOf(ActionForm.getCurrentWeek()))){
					ActionForm.setCurrentWeek(rest.getInt("sysdate"));
				}
			}
			
//			未检索到数据
			if(map == null){
				ActionForm.setErrorFlag("2");
				ActionForm.setCurrentWeek(0);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			ActionForm.setErrorFlag("3");
		} finally {
			Dispose.dispose(rest);
		}
		return rtnList;
	}
}
